package com.ssm_basic.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ssm_basic.egweibo2.base.response.CodeMsg;
import com.ssm_basic.egweibo2.base.response.JsonResult;
import com.ssm_basic.egweibo2.base.utils.JwtUtil;
import com.ssm_basic.egweibo2.mapper.UserMapper;
import com.ssm_basic.egweibo2.pojo.dto.UserLoginParam;
import com.ssm_basic.egweibo2.pojo.dto.UserRegParam;
import com.ssm_basic.egweibo2.pojo.entity.User;
import com.ssm_basic.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "1.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/reg/")
public class UserController {

    @Autowired
    private UserMapper  userMapper;

    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 5)
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam) {
        log.debug("userRegParam:{}", userRegParam);
        String username = userRegParam.getUsername();
        int i = userMapper.selectByUsername(username);
        if (i > 0) {
            return new JsonResult(CodeMsg.USERNAME_EXIST);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.success();
    }

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("login")
    public JsonResult login(@RequestBody @Validated UserLoginParam param, @ApiIgnore HttpSession session) {
        log.debug("UserLoginParam:" + param);
        UserVO userVO = userMapper.selectByUAP(param);
        if (userVO == null) {
            return new JsonResult(CodeMsg.USERNAME_PASSWORD_ERROR);
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",userVO.getId());
        claims.put("username",userVO.getUsername());
        String token = JwtUtil.genToken(claims);
        session.setAttribute("user", token);
        return JsonResult.success(userVO);
    }

    @ApiOperation("获取当前用户")
    @ApiOperationSupport(order = 15)
    @GetMapping("current")
    public JsonResult current(@ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(CodeMsg.NOT_LOGIN);
        }
        return JsonResult.success(userVO);
    }

    @ApiOperation("用户退出")
    @ApiOperationSupport(order = 20)
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session) {
        session.removeAttribute("user");
    }

}
